﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;

namespace AppFx.BizTalk
{
    /// <summary>
    /// This is used to allow validation 
    /// </summary>
    public sealed class Ensure
    {
        
        /// <summary>
        /// Creates an exception of the appropriate type
        /// </summary>
        /// <param name="exceptionType"></param>
        /// <param name="failureMessage"></param>
        /// <returns></returns>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Globalization", "CA1303:DoNotPassLiteralsAsLocalizedParameters", MessageId = "System.ArgumentException.#ctor(System.String)")]
        private static void ThrowException(Type exceptionType, string failureMessage)
        {
            ConstructorInfo ctor = exceptionType.GetConstructor(new Type[] { typeof(string) });
            if (ctor == null)
                throw new ArgumentException("The exception type does not have a valid constructor which accepts a single string input parameter");

            object o = ctor.Invoke(new object[] { failureMessage });
            Exception ex = o as Exception;
            if (ex != null)
            {
                throw ex;
            }
            else
                throw new ArgumentException("The exception type you have specified does not derive from System.Exception");
        }
        /// <summary>
        /// If the evalueation is false then an error is thrown
        /// </summary>        
        /// <param name="failureMessage"></param>
        /// <param name="exceptionType"></param>
        /// <param name="result"></param>
        public static void IsTrue(bool result, string failureMessage, Type exceptionType)
        {            
            if (result == false)
                ThrowException(exceptionType, failureMessage);
        }
        /// <summary>
        /// Evaluates the expression and throws an error if it is true
        /// </summary>
        /// <param name="result"></param>
        /// <param name="failureMessage"></param>
        /// <param name="exceptionType"></param>
        public static void IsFalse(bool result, string failureMessage, Type exceptionType)
        {
            if (result == true)
                ThrowException(exceptionType, failureMessage);
        }
        /// <summary>
        /// Throws an argumentnull exception if the object is null
        /// </summary>
        /// <param name="o"></param>
        /// <param name="failureMessage"></param>
        /// <param name="exceptionType"></param>
        public static void IsNotNull(object o, string failureMessage, Type exceptionType)
        {
            if (o == null)
                ThrowException(exceptionType, failureMessage);
        }
        /// <summary>
        /// Throws exception if the string is null or empty
        /// </summary>
        /// <param name="s"></param>
        /// <param name="failureMessage"></param>
        /// <param name="exceptionType"></param>
        public static void IsNotNullOrEmpty(string s, string failureMessage, Type exceptionType)
        {
            if(string.IsNullOrEmpty(s))
                ThrowException(exceptionType, failureMessage);
        }
    }
}


